﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PEC.Forms
{
    public partial class FrmChuyen : Form
    {
        public FrmChuyen()
        {
            InitializeComponent();
        }

        private void FrmChuyen_Load(object sender, EventArgs e)
        {
            List<string> s = new List<string>();
            for (int i = 1; i <= PecController.Current.SoChuyen; i++)
            {
                s.Add(i.ToString());
            }
            cbLine.DataSource = s;
        }

        private void cbLine_SelectedValueChanged(object sender, EventArgs e)
        {
            string sErr = "";
            string line = cbLine.Text;
            DTO.ChuyenSanXuatInfo inf = null;
            if (!string.IsNullOrEmpty(line))
            {
                BUS.ChuyenSanXuatControl ctr = new BUS.ChuyenSanXuatControl(PecController.Current.Bus);
                DTO.FilterParameterCollection fs = new DTO.FilterParameterCollection();
                fs.Add(DTO.ChuyenSanXuatInfo.Field.LineId, Convert.ToInt32(line));
                DTO.ChuyenSanXuatInfo[] infs = ctr.GetByFilter(PecController.Current.XuongId, fs, ref sErr);
                if (infs == null || infs.Length == 0)
                {
                    inf = new DTO.ChuyenSanXuatInfo()
                    {
                        XuongId = PecController.Current.XuongId,
                        LineId = Convert.ToInt32(line)
                    };
                }
                else
                {
                    inf = infs[0];
                }
                SetData(inf);
            }
            else MessageBox.Show("Choose line please");
        }

        private void SetData(DTO.ChuyenSanXuatInfo inf)
        {
            chuyenSanXuatInfoBindingSource.DataSource = new List<DTO.ChuyenSanXuatInfo> { inf };
            chuyenSanXuatInfoBindingSource.Position = 0;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            DTO.ChuyenSanXuatInfo inf = GetData();
            
            BUS.ChuyenSanXuatControl ctr = new BUS.ChuyenSanXuatControl(PecController.Current.Bus);
            string sErr = ctr.InsertUpdate(inf);
            if (!string.IsNullOrEmpty(sErr))
                MessageBox.Show(sErr, "Error");
            else MessageBox.Show("Save successfully");
        }

        private DTO.ChuyenSanXuatInfo GetData()
        {
            List<DTO.ChuyenSanXuatInfo> ds = chuyenSanXuatInfoBindingSource.DataSource as List<DTO.ChuyenSanXuatInfo>;
            if (ds == null) return null;
            DTO.ChuyenSanXuatInfo inf = ds[0];
            inf.XuongId = PecController.Current.XuongId;
            inf.LineId = Convert.ToInt32(cbLine.Text);
            return inf;
        }

        private void ndfTotalQty_ValueChanged(object sender, EventArgs e)
        {
            ndfTotalAmt.Value = ndfTotalQty.Value * ndfPrice.Value;
        }

        private void ndfPrice_ValueChanged(object sender, EventArgs e)
        {
            ndfTotalAmt.Value = ndfTotalQty.Value * ndfPrice.Value;
            ndfAmt.Value = ndfActual.Value * ndfPrice.Value;
        }

        private void ndfActual_ValueChanged(object sender, EventArgs e)
        {
            ndfAmt.Value = ndfActual.Value * ndfPrice.Value;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btCopy_Click(object sender, EventArgs e)
        {
            if (cbLine.SelectedIndex < PecController.Current.SoChuyen - 1)
            {
                DTO.ChuyenSanXuatInfo inf = GetData();

                cbLine.SelectedIndex += 1;
                if (inf != null)
                {
                    DTO.ChuyenSanXuatInfo inf1 = GetData();
                    inf.LineId = inf1.LineId;
                    SetData(inf);

                }
            }
        }
    }
}
